/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import static Servlets.InicioAction.con;
import beans.LoginForm;
import beans.clientesForm;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.LoginBean;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author RICARDO
 */
public class InsertarAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    static Connection con  = null;
    static Statement  st   = null;  
    static ResultSet  res  = null; 
    static String     sql  = null;
    private static final String DBH = "jdbc:hsqldb:mem:memoriadb";
            boolean status;
            int i;
    private static String nombre;
    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
                  //crearemos una BD en memoria
       clientesForm  formulario=(clientesForm) form;
        nombre=formulario.getNombre();
        LoginBean lb=new LoginBean();//copiar el form de la vista en un objeto de modelo
        //PropertyUtils.copyProperties(lb,formulario);
     
        //formulario.reset();
        System.out.println("hoooooola Enriquuuuuuuuuue");
        loadHSQLDB();
        connectDB();
        createStatement();
        insertData();
        //closeHSQLDB();
          //hola ricardito      
       request.setAttribute("cliente",formulario);
 
        return mapping.findForward(SUCCESS);
        //return mapping.findForward("prueba");
    }
    
    public static void loadHSQLDB()
    {
        System.out.println("* Starting...");
        try
        {
            Class.forName("org.hsqldb.jdbcDriver"); //Load HSQLDB driver
            System.out.println("* Loading HSQLDB driver...");
        } 
        catch(Exception e)
        {  
            System.err.println("ERROR: failed to load HSQLDB JDBC driver.");
            e.printStackTrace();
            return;  
        } 
    }
            public static void connectDB()
    {
        try
        {   // Connect to the database or create if it don't exist 
            con = DriverManager.getConnection(DBH); 
            System.out.println("* Creating HSQLDB connection...");
        }
        catch(Exception e)
        {
            System.err.println("Error: " + e.getMessage());
            return;
        }
    }
          public static void createStatement()
    {
        try
        {
            st  = con.createStatement(); 
        }
        catch (Exception e)
        {  
            System.err.println("Error: createStatement: " + e.getMessage());   
            return;
        }
    }  
        

    
        public static void insertData()
    {
        // Insert data  
        System.out.println("Insertando datos en tablas");
        try 
        { 
            //Insert subject
            //st.executeUpdate("INSERT INTO subject VALUES ('3', 'Matematicas')");  
  
 //nombre="Enrique";
            //Insert student
            //st.executeUpdate("INSERT INTO cliente VALUES ('3', 'Ricardo', 'Asensi', 'Sol, 12', 'Sevilla', '13207', 'j.garrido@gmail.com', '609555321', '0')"); 
            st.executeUpdate("INSERT INTO clientes (nombre, apellido_1,contrasena,direccion,ciudad,codigopostal,email,telefono) VALUES ('"+nombre+"', 'Asensi','1234', 'Sol, 12', 'Sevilla', '13207', 'j.garrido@gmail.com', '609555321')"); 
              
 
            //Foreign keys stusub
         //  st.executeUpdate("INSERT INTO stusub VALUES ('3', '0', '2')");  
     
        }
        catch (Exception e)
        {  
            System.err.println("Warning: error on insert data: " + e.getMessage());   
            e.printStackTrace();
            return; 
        } 
    }
    
    public static void closeHSQLDB()
    {
        // Save temporal data and close
        try
        {
            st = con.createStatement();  
            st.executeUpdate("SHUTDOWN");  
            st.close(); 
        }
        catch(Exception e)
        {
            System.out.println("Error: save temporal data" + e.getMessage());
        }
 
        try
        {
            con.close();
        }
        catch(Exception e)
        {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
            return; 
        }
    }
}
